﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MyMovieInfo
{
    class MovieResolver: IMovieResolution
    {
        private List<Movie> _movies = new List<Movie>();
        //private readonly IList<ILookupService> _lookupServices = new List<ILookupService>();
        private IPersistenceService _persistenceService;
        private IList<IMovieLookup> _lookupServices;
        private IMovieListCreator _queryService;

        public MovieResolver( IMovieListCreator queryService, IList<IMovieLookup> lookupservices, IPersistenceService persistence)
        {
            this._queryService = queryService;
            this._lookupServices = lookupservices;
            this._persistenceService = persistence;
        }


        public void RegisterLookupService(IMovieLookup service)
        {
            _lookupServices.Add(service);
        }


        public List<Movie> Movies
        {
            get { return this._movies; }
        }


        public void ExecuteQueries()
        {
            var queries = _queryService.CreateMovieQueries();

            foreach (var query in queries)
            {
                var movie = new Movie();
                foreach (IMovieLookup service in _lookupServices)
                {
                    movie = service.GetMovieDetails(query.Name, query.Year, query.FilePath);
                    if (movie.MatchFound)
                        break;
                }
                _movies.Add(movie);
            }
        }

        public void SaveResults()
        {
            _persistenceService.SaveMovieList(_movies);
        }
    }
}